from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers.primitive import String, Integer, Array
from soaplib.serializers.clazz import ClassSerializer
from tg import expose
class Permission(ClassSerializer):
    class types:
        application = String
        feature = String

class User(ClassSerializer):
    class types:
        userid = Integer
        username = String
        firstname = String
        lastname = String
        permissions = Array(Permission)
user = User()
user.userid = 1
user.username = "macio"
user_database = {1:user}
userid_seq = 1


class UserManager(SimpleWSGISoapApp):
    """ this is test of add_user finction """
    
    @soapmethod(User,_returns=Integer)
    def add_user(self,user):
        global user_database
        global userid_seq
        user.userid = userid_seq
        userid_seq = user_seq+1
        user_database[user.userid] = user
        return user.userid
    @soapmethod(Integer,_returns=User)
    def get_user(self,userid):
        global user_database
        return user_database[userid]
    @soapmethod(User)
    def modify_user(self,user):
        global user_database
        user_database[user.userid] = user
    @soapmethod(Integer)
    def delete_user(self,userid):
        global user_database
        del user_database[userid]
    @soapmethod(_returns=Array(User))
    def list_users(self):
        global user_database
        return [v for k,v in user_database.items()]

SoapController = UserManager()